#!/usr/bin/env python

import cgi
import wsgiref.handlers
import os
from google.appengine.api import users
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app

from google.appengine.api.labs import taskqueue
from google.appengine.api import urlfetch
import xml.etree.ElementTree as ET

from google.appengine.api import memcache

from model.db_dou_people import DbDouPeople
from model.db_dou_people import DbDouPeopelIdx
import logging
import urllib

class ControlDouInit(webapp.RequestHandler):
    def get(self):
        #=======================================================================
        # for i in range(10):
        #     dbdoupeople = DbDouPeople()
        #    dbdoupeople.db_uid = 'samhooxee'
        #    dbdoupeople.id_from = 'origin'
        #    dbdoupeople.type = 0
        #    dbdoupeople.flag_contact = False
        #    dbdoupeople.put()
        #=======================================================================
        self.response.out.write("ControlDouInit do nothing!!!")

class ControlDouInitidx(webapp.RequestHandler):
    def get(self):
        #q = DbDouPeople.all()
        #logging.info("%d" % q.count())
        #for i in q:
        #    i.type = 0
        #    i.put()
        q = DbDouPeopelIdx.all().get()
        q.cnt = 0
        q.put()

class ControlDouSetidxTask(webapp.RequestHandler):
    def post(self):
        idx = self.request.get('idx')
        q2 = DbDouPeople.gql("WHERE type = 0")
        for item in q2.fetch(666):
            item.type = int(idx)
            item.put()

class ControlDouSetidx(webapp.RequestHandler):
    def get(self):
        q = DbDouPeopelIdx.all()
        if q.count()==0:
            #db_pidx = DbDouPeopelIdx()
            #db_pidx.type = 1
            #db_pidx.put()
            logging.info("DbDouPeopelIdx should only be single")
            return
        else:
            db_pidx = q.get()
            logging.info("DbDouPeopelIdx get single")
        q2 = DbDouPeople.gql("WHERE type = 0")
        if q2.count()>666:
            idx_str = "%d" % db_pidx.type
            logging.info("ControlDouSetidx %s" % idx_str)
            taskqueue.add(url='/controlDouSetidxTask', params={'idx': idx_str})
            db_pidx.type += 1
            db_pidx.put()
        else:
            logging.info("ControlDouSetidx not enough %d" % q2.count())
            self.response.out.write("ControlDouSetidx not enough %d" % q2.count())

class ControlDouGetidxOld(webapp.RequestHandler):
    def get(self):
        #idx_str = self.request.get('idx')
        #idx = int(idx_str)
        qidx = DbDouPeopelIdx.all().get()
        for i in range(qidx.type):
            q = DbDouPeople.gql("WHERE type = :1", i)
            self.response.out.write("%d + " % q.count())

class ControlDouGetidx(webapp.RequestHandler):
    def get(self):
        f_str = self.request.get('f')
        f = int(f_str)
        t_str = self.request.get('t')
        t = int(t_str)
        #qidx = DbDouPeopelIdx.all().get()
        for i in range(f, t):
            q = DbDouPeople.gql("WHERE type = :1", i)
            for item in q:
                self.response.out.write("%s, %s, %s, %d<br/>" % (item.db_uid, item.id_from, item.title, item.type) )

class ControlDouDel(webapp.RequestHandler):
    def get(self):
        #dbdoupeople_query = DbDouPeople.all()
        #db.delete(dbdoupeople_query)
        logging.info("ControlDouDel do nothing!")

def urlfetch_getresult(url):
    try:
        result = urlfetch.fetch(url=url)
        return result
    except:
        logging.info('url err %s' % url)
        #result = urlfetch_getresult(url)
        return 

class ControlDouGetTask(webapp.RequestHandler):
    def post(self):
        db_uid = self.request.get('db_uid')
        idx_str = self.request.get('idx_str')
        url = "http://api.douban.com/people/" + db_uid + "/contacts?start-index=" + idx_str + "&max-results=44"
        logging.info("%s, %s" % (db_uid, idx_str))
        result = urlfetch_getresult(url=url)
        try :
            result_xml = ET.fromstring(result.content)
            items = result_xml.findall('{http://www.w3.org/2005/Atom}entry')
            for item in items:
                c_db_uid = item.find('{http://www.douban.com/xmlns/}uid').text
                qdft = DbDouPeople.gql("WHERE db_uid = :db_uid ",db_uid=c_db_uid)
                if qdft.count() == 0:
                    c_title = item.find('{http://www.w3.org/2005/Atom}title').text
                    dbdoupeople = DbDouPeople()
                    dbdoupeople.db_uid = c_db_uid
                    dbdoupeople.id_from = db_uid
                    dbdoupeople.flag_contact = False
                    dbdoupeople.title = c_title
                    dbdoupeople.type = 0
                    dbdoupeople.put()
                else:
                    logging.info('duplicate title %s' % qdft.get().title)
        except:
            logging.info("ControlDouGet error!!!")

class ControlDouGet(webapp.RequestHandler):
    def get(self):
        #dbdoupeople_query = DbDouPeople.all()
        #dbdoupeople_query.filter('flag_contact != ', True)
        dbdoupeople_query = DbDouPeople.gql("WHERE flag_contact != :flag_contact ",flag_contact=True)
        cnt = 0
        for people in dbdoupeople_query:
            if cnt > 2000:
                break
            db_uid = people.db_uid
            url = "http://api.douban.com/people/" + db_uid + "/contacts"
            result = urlfetch_getresult(url=url)
            try:
                result_xml = ET.fromstring(result.content)
                total = result_xml.find('{http://a9.com/-/spec/opensearchrss/1.0/}totalResults')
            except:
                logging.info('url err 2 %s' % url)
                continue
            people.flag_contact = True
            people.put()
            total_int = int(total.text)
            page_int = total_int/44
            if total_int%44:
                page_int += 1
            for idx in range(page_int):
                idx_str = "%d" % (idx*44+1)
                total_str = "%d" % total_int
                taskqueue.add(url='/controlDouGetTask', params={'db_uid': db_uid, 'idx_str': idx_str})
            cnt += total_int
            logging.info('cnt 1 - %d' % cnt)
        logging.info('cnt 2 - %d' % cnt)

class ControlDouBigquery(webapp.RequestHandler):
    def get(self):
        form_fields = {
          "q": 'SELECT title,type FROM [dbdoupeople/dou01db] WHERE type= 1 LIMIT 100;',
          #"method": "bigquery.query"
        }
        form_data = urllib.urlencode(form_fields)
        header_fields = {
            #'Content-Type': 'application/json',
            'Authorization': 'GoogleLogin auth=DQAAAKwAAAAQnhAE5hfpFnMfZ37Wbz-HhAYRQlfXeqzuC7vbI8PtApgABjSq9yk6KbWtm1YpHjuET5qujb5vw9daMmZGemarl6XiNtA2i0HX_pFtQukrOlCd3zn4YypNmgp0Emja44ylsd33Sw1bThHSGMGBpiaCwfITKXCR6P8stsyuSk5NWU1QP95sMKLNkTKK1CS30ssGx1kRjCE2uAyXZDykh8-zxTkJ6hbdWR7u-YbWEsyUqw'
        }
        url0 = 'https://www.googleapis.com/bigquery/v1/query?'
        #url1 = 'https://www.googleapis.com/bigquery/v1/query?q=SELECT+title,type+FROM+[dbdoupeople/dou01db]+LIMIT+100;'
        #url2 = 'https://www.googleapis.com/bigquery/v1/query?q=SELECT title,type FROM [dbdoupeople/dou01db] LIMIT 100;'
        #urlx = 'https://www.googleapis.com/bigquery/v1/tables/dbdoupeople/dou01db'
        result = urlfetch.fetch(url=url0+form_data,
                                    #payload=form_fields,
                                    #method=urlfetch.POST,
                                    headers=header_fields)
        self.response.out.write(url0+form_data)
        self.response.out.write(result.content)
        pass

def main():
  application = webapp.WSGIApplication([('/controlDouInit', ControlDouInit),
                                        ('/controlDouGet', ControlDouGet),
                                        ('/controlDouGetTask', ControlDouGetTask),
                                        ('/controlDouDel', ControlDouDel),
                                        ('/controlDouSetidx', ControlDouSetidx),
                                        ('/controlDouSetidxTask', ControlDouSetidxTask),
                                        ('/controlDouGetidx', ControlDouGetidx),
                                        ('/controlDouInitidx', ControlDouInitidx),
                                        ('/controlDouBigquery', ControlDouBigquery),
                                        ], debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
  main()